package com.example.wtt.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.wtt.common.Constants;
import com.example.wtt.common.Result;
import com.example.wtt.controller.dto.UserDTO;
import com.example.wtt.entity.User;
import com.example.wtt.entity.Validation;
import com.example.wtt.exception.ServiceException;
import com.example.wtt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/user") // 统一给接口加前缀
public class UserController {

    @Autowired
    private UserService userService;

    private static String flag = "";

    @PostMapping("/login")
    public Result login(@RequestBody UserDTO userDTO, HttpServletRequest request) throws Exception {
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();

        if(StrUtil.isBlank(username) || StrUtil.isBlank(password)){
            return Result.error(Constants.CODE_400,"参数错误");
        }
//        else if(!flag.equals(userDTO.getCode())){
//            return Result.error(Constants.CODE_400,"验证码错误");
//        }
        UserDTO dto = userService.login(userDTO);
        return Result.success(dto);
    }

    @PostMapping("/loginEmail")
    public Result loginEmail(@RequestBody UserDTO userDTO, HttpServletRequest request) throws Exception {
        String email = userDTO.getEmail();
        String emailCode = userDTO.getEmailCode();

        if(StrUtil.isBlank(email) || StrUtil.isBlank(emailCode)){
            return Result.error(Constants.CODE_400,"参数错误");
        }
        UserDTO dto = userService.loginEmail(userDTO);
        return Result.success(dto);
    }


    @GetMapping("/email/{email}")
    public Result sendEmailCode(@PathVariable String email) throws Exception {
        if(StrUtil.isBlank(email)){
            throw new ServiceException(Constants.CODE_400, "参数错误");
        }

        userService.sendEmailCode(email);
        return Result.success();
    }

    @PostMapping("/rEmail")
    public Result rSendEmailCode(@RequestBody Validation validation) throws Exception {
        if(StrUtil.isBlank(validation.getEmail())){
            throw new ServiceException(Constants.CODE_400, "参数错误");
        }

        userService.rSendEmailCode(validation);
        return Result.success();
    }

    @PostMapping("/register")
    public Result register(@RequestBody UserDTO userDTO) throws Exception {
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        String email = userDTO.getEmail();
        String emailCode = userDTO.getEmailCode();

        if(StrUtil.isBlank(username) || StrUtil.isBlank(password) || StrUtil.isBlank(email) || StrUtil.isBlank(emailCode)){
            return Result.error(Constants.CODE_400,"参数错误");
        }
        return Result.success(userService.register(userDTO));
    }

    //增、改接口
    @PostMapping
    public Result save(@RequestBody User user){
        return Result.success(userService.saveOrUpdate(user));
    } // 将前端传来的json转换为后端的java对象

    @GetMapping("/userid/{userid}")
    public Result userInfo(@PathVariable Integer userid){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",userid);
        return Result.success(userService.getOne(queryWrapper));
    }

    @GetMapping("/username/{username}")
    public Result findOne(@PathVariable String username){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return Result.success(userService.getOne(queryWrapper));
    }

    @GetMapping
    public Result findHome(){
        return Result.success(userService.findHome());
    }

    @GetMapping("/dynamicCollect/{userId}")
    public Result dynamicCollect(@PathVariable Integer userId){
        return Result.success(userService.dynamicCollect(userId));
    }

    @GetMapping("/becomeVip/{userId}")
    public Result becomeVip(@PathVariable Integer userId){
        return Result.success(userService.becomeVip(userId));
    }

}
